<div class="content">
    <div class="right">
        <div id="sf_admin_container">
            <div id="search_box">
                <div class="sf_admin_filters">
                    <form action="/customer_care/sms?page=1" method="POST">
                        <fieldset>
                            <h2>Tìm kiếm</h2>
                            <div class="form-row">
                                <label>Số điện thoại: </label>
                                <input type="text" name="phone_number" value ="<?php echo isset($phoneNumber) ? $phoneNumber : null?> "/>
                            </div>
                            <div class="form-row">
                                <label>Loại tin nhắn: </label>
                                <select name="sms_type">
                                    <option <?php if($smsType == "Ra"){echo("selected");}?>>Ra</option>
                                    <option <?php if($smsType == "Vào"){echo("selected");}?>>Vào</option>
                                </select>
                            </div>
                            <div class="form-row">
                                <label for="filters_created_datetime">Ngày tạo</label>
                                <div class="content">
                                    <?php
                                        $name = 'created_datetime';
                                        $value1 = isset($createdDatetimeFrom) ? $createdDatetimeFrom : null;
                                        $value2 = isset($createdDatetimeTo) ? $createdDatetimeTo : null;
                                        $options = array (
                                                                  'rich' => true,
                                                                  'withtime' => true,
                                                                  'calendar_button_img' => '/sf/sf_admin/images/date.png',
                                                                );
                                        $options = _parse_attributes($options);
                                        $before = _get_option($options, 'before', '');
                                        $middle = _get_option($options, 'middle', '');
                                        $after  = _get_option($options, 'after', '');
                                    ?>
                                    <?php
                                        echo $before.
                                         input_date_tag($name.'_from',$value1 , $options).
                                         $middle;
                                    ?>
                                    <?php
                                        echo input_date_tag($name.'_to', $value2, $options).
                                             $after;
                                    ?>

                                </div>
                            </div>
                            <div style="clear: both; margin-left: 120px;" class="form-row">
                                <input type="submit" style="width: 80px;" value="Tìm kiếm" name="filter"/>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
            <div id="sf_admin_content">
                <?php if($sms):?>
                <table class="sf_admin_list" cellspacing="0">
                    <thead>
                        <tr>
                            <th>Id</th>
                            <th>Số dịch vụ</th>
                            <th>SmsId</th>
                            <th>Số thuê bao gửi</th>
                            <th>Thời gian nhận</th>
                            <th>Nội dung</th>
                            <th>Trạng thái</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php for($i = 0; $i<count($sms); $i ++):?>
                        <tr class="sf_admin_row_<?php echo $i?>">
                             <td>
                                <?php echo $sms[$i]['id'];?>
                            </td>
                            <td>
                                <?php echo $sms[$i]['serviceNumber'];?>
                            </td>
                            <td>
                                <?php echo $sms[$i]['smsId'];?>
                            </td>
                            <td>
                                <?php echo $sms[$i]['phoneNumber'];?>
                            </td>
                            <td>
                                <?php echo $sms[$i]['createdDatetime'];?>
                            </td>
                            <td>
                                <?php echo $sms[$i]['content'];?>
                            </td>
                            <td>
                                <?php 
                                    //echo $sms[$i]['status'];
                                	if($sms[$i]['status'] == 0)
                                	{
                                		echo "success";
                                	}
                                	else 
                                	{
                                		echo "fail";
                                	}
                                ?>
                            </td>
                        </tr>
                        <?php endfor;?>
                    </tbody>
                </table>
                <b>Có <?php echo $totalSms;?> kết quả</b>
                <?php if ($totalPage > 1): ?>
                            <div class= "clearfix" align="right">
                                    <div class="paging_comment" >
                                            <?php
                                                $url = "/customer_care/sms?phone_number=".$phoneNumber."&created_datetime_from=".$createdDatetimeFrom."&created_datetime_to=".$createdDatetimeTo."&sms_type=".$smsType."&page=%s";
                                                echo showPage_1($totalPage, $page, $url);
                                            ?>
                                    </div>
                            </div>
                        <?php endif; ?>
                <?php endif; ?>
            </div>
    </div>
</div>
    <?php
    function paging($totalPage, $currentPage, $minPage, $previousPage, $nextPage, $maxPage)
	{
	    $minPage = ($currentPage - 3 > 1 ? ($currentPage - 3) : 1);
	    $maxPage = ($currentPage + 3 < $totalPage ? ($currentPage + 3) : $totalPage);
	    $previousPage = $currentPage - 1;
	    $nextPage = $currentPage + 1;
	}
    function showPage_1($totalPage, $page, $url)
	{
	    if ($totalPage > 1)
	    {
	        $minPage = 0;
	        $maxPage = 0;
	        $previousPage = 0;
	        $nextPage = 0;
	        paging($totalPage, $page, &$minPage, &$previousPage, &$nextPage, &$maxPage);

	        $urlToFirst = sprintf($url, 1);
	        $urlToPrevious = sprintf($url, $previousPage);
	        $urlToNext = sprintf($url, $nextPage);
	        $urlToLast = sprintf($url, $totalPage);

	        if ($minPage == 1)
	        {
	            $linkToFirst = '';
	        }
	        else
	        if ($minPage == 2)
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>';//link_to(1, $urlToFirst);
	        }
	        else
	        {
	            $linkToFirst = '<a href="'.$urlToFirst.'">1</a>...';//link_to(1, $urlToFirst).'...';
	        }

	        //$linkToPrevious = ($previousPage < 1 ? '' : link_to(PAGE_PREVIOUS, $urlToPrevious));

	        if ($maxPage == $totalPage)
	        {
	            $linkToLast = '';
	        }
	        else
	        if ($maxPage == $totalPage - 1)
	        {
	            $linkToLast = '<a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast);
	        }
	        else
	        {
	            $linkToLast = '<li>...</li><li><a href="'.$urlToLast.'">'.$totalPage.'</a>';//link_to($totalPage, $urlToLast).'</li>';
	        }

	        //$linkToNext = ($nextPage > $totalPage ? '' : link_to(PAGE_NEXT, $urlToNext));
	        $linkToPage = '';
	        for ($i = $minPage; $i <= $maxPage; $i++)
	        {
	            $urlToPage = sprintf($url, $i);
	            $linkToPage.=($i == $page ?'<li><a class="page_current">'.$i.'</a></li>' :'<li><a href="'.$urlToPage.'">'.$i.'</a></li>'.'');
	        }
	        echo  '<div>Trang: '.
	        '
	                     <ul>
	                         <li>'.$linkToFirst.'</li>'.
	        $linkToPage.
	        '<li>'.$linkToLast.'</li>
	                     </ul>
	                   </div>
	               ';
	    }
	}
    ?>
